package com.frozenlab.android.weather.adapter;

import java.io.IOException;

import org.w3c.dom.Document;

import android.util.Log;

import com.frozenlab.android.weather.helper.HttpConnectHelper;
import com.frozenlab.android.weather.helper.WundergroundWeatherHelpher;
import com.frozenlab.android.weather.helper.YahooWeatherHelper;
import com.frozenlab.android.weather.vo.WeatherVO;

public class WeatherAdapter {
	
	private static final String TAG = "WeatherAdapter";
	
	private HttpConnectHelper httpConnectHelper;
	private String strURL;
	private WeatherVO weatherVO;
	
	public WeatherAdapter() {
		this.weatherVO = new WeatherVO();
		this.httpConnectHelper = new HttpConnectHelper();
	}

	public WeatherVO getWeatherByWOEID(String woeid) {
		// verify parameter
		if (woeid == null){
			Log.e(TAG,"WOEID is NULL");
			return null;
		}

		strURL = YahooWeatherHelper.createUrl(woeid);
		try {
			Document doc = httpConnectHelper.getDocumentFromData(strURL);
			
			if (doc != null){
				Log.e(TAG,"Parsing WOEID from data");
				weatherVO = YahooWeatherHelper.parseYahooWeatherInfo(doc);
			}
		} catch (IOException e) {
			Log.e(TAG, e.toString());
		}
		
		return weatherVO;
	}
	
	
	public WeatherVO getWeatherByCoordinate(String latitude, String longitude){
		// verify parameter
		if (latitude == null || longitude == null){
			Log.e(TAG,"Param is NULL");
			return null;
		}
		
		strURL = WundergroundWeatherHelpher.createUrl(latitude, longitude);
		
		String strData;
		try {
			strData = httpConnectHelper.getDataFromUrl(strURL);

			if (strData != null) {
				Log.e(TAG, "JSON Parsing from data");
				weatherVO = WundergroundWeatherHelpher.parseWundergroundWeatherJSON(strData);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return weatherVO;
	}

}
